package com.twq.types.typeparam.covariant

import com.twq.types.typeparam.{Animal, Cat, Dog}

/**
 * Created by tangweiqun on 2017/12/7.
 */
object CovarianceTest {
  def main(args: Array[String]): Unit = {
    def printAnimalNames(animals: List[Animal]): Unit = {
      animals.foreach { animal =>
        println(animal.name)
      }
    }

    val cats: List[Cat] = List(new Cat("Whiskers"), new Cat("Tom"))
    val dogs: List[Dog] = List(new Dog("Fido"), new Dog("Rex"))

    //List[Cat]是List[Animal]的子类
    printAnimalNames(cats)
    // Whiskers
    // Tom

    //List[Dog]是List[Animal]的子类
    printAnimalNames(dogs)
    // Fido
    // Rex

  }
}
